package com.ice.body.business.controller;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.ice.base.model.SysLoginModel;
import com.ice.base.util.BaseUtil;
import com.ice.body.business.model.RecordLab;
import com.ice.body.business.service.RecordLabService;
import io.swagger.annotations.ApiOperation;
import com.ice.base.controller.BaseController;
import com.github.pagehelper.PageHelper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.ice.base.result.ResponseResult;
import com.ice.base.result.AjaxResult;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;

/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2021-10-25
 * Time: 10:32
 */
@RestController
@ApiIgnore(value = "产品追溯记录")
public class RecordLabController extends BaseController{
    //---------------------------产品追溯记录-----------------------start
    @Resource
    private RecordLabService recordLabService;
    


    @ApiOperation(value = "产品追溯记录-删除")
    @PostMapping("/api/ReagentRecord/deleteById")
    public AjaxResult<RecordLab> deleteById_ReagentRecord(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            Integer count = recordLabService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }


    @ApiOperation(value = "产品追溯记录-查询多个")
    @PostMapping("/api/ReagentRecord/selectAll")
    public AjaxResult<RecordLab> selectList_ReagentRecord(RecordLab model) {
        AjaxResult r = ResponseResult.success();
        try{
           List<RecordLab> list = recordLabService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "产品追溯记录-查询分页")
    @PostMapping("/api/RecordLab/retrace")

    public AjaxResult<RecordLab> selectListByPageHelper_ReagentRecord(@RequestParam(name = "pageSize") Integer pageSize,
                                                                      @RequestParam(name = "pageNum") Integer pageNum,
                                                                      RecordLab model){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           list = recordLabService.selectListByModel(model);
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------产品追溯记录-----------------------end

    @ApiOperation(value = "试剂操作记录-入库-查询分页")
    @PostMapping("/api/RecordLab/selectList")
    public AjaxResult<RecordLab> selectList(@RequestParam(name = "pageSize") Integer pageSize,
                                                   @RequestParam(name = "pageNum") Integer pageNum,
                                                   RecordLab model){
        AjaxResult r = ResponseResult.success();
        ArrayList list;

        try{
            PageHelper.startPage(pageNum, pageSize);
            list = recordLabService.selectListByModel(model);
            getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();
        }
        return r;
    }

   /* @ApiOperation(value = "试剂操作记录-出库-查询分页")
    @PostMapping("/api/RecordLab/selectListOutStock")
    public AjaxResult<RecordLab> selectListOutStock(@RequestParam(name = "pageSize") Integer pageSize,
                                                    @RequestParam(name = "pageNum") Integer pageNum,
                                                    RecordLab model){
        AjaxResult r = ResponseResult.success();

        SysLoginModel currLoginModel = BaseUtil.getCurrLoginModel();
        if(!"admin".equals(currLoginModel.getUserName())){
            model.setDeptId(currLoginModel.getDeptId());
        }
        ArrayList list;
        model.setState(2);
        try{
            PageHelper.startPage(pageNum, pageSize);
            list = recordLabService.selectListByModel(model);
            getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();
        }
        return r;
    }

    @ApiOperation(value = "试剂操作记录-归还-查询分页")
    @PostMapping("/api/RecordLab/selectListBackStock")
    public AjaxResult<RecordLab> selectListBackStock(@RequestParam(name = "pageSize") Integer pageSize,
                                                     @RequestParam(name = "pageNum") Integer pageNum,
                                                     RecordLab model){
        AjaxResult r = ResponseResult.success();

        SysLoginModel currLoginModel = BaseUtil.getCurrLoginModel();
        if(!"admin".equals(currLoginModel.getUserName())){
            model.setDeptId(currLoginModel.getDeptId());
        }
        ArrayList list;
        model.setState(3);
        try{
            PageHelper.startPage(pageNum, pageSize);
            list = recordLabService.selectListByModel(model);
            getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();
        }
        return r;
    }*/



    @ApiOperation(value = "试剂使用记录-导出到response")
    @GetMapping("/api/RecordLab/exportRecord")
    public void exportRecordLab(HttpServletResponse response,
                           @RequestParam(name = "epcCode") String   epcCode ) {
        try {
            List rows = recordLabService.getListByEpcCode(epcCode);
            // 通过工具类创建writer
            ExcelWriter writer = ExcelUtil.getWriter();
            //自定义标题别名
            writer.addHeaderAlias("serialNo", "序列号");
            writer.addHeaderAlias("epcCode", "epc编码");
            writer.addHeaderAlias("userName", "操作人");
            writer.addHeaderAlias("createTime", "操作时间");
            writer.addHeaderAlias("deptId", "部门id");
            writer.addHeaderAlias("cName", "危化品名称");
            writer.addHeaderAlias("state", "1入库2出库3归还0销毁");

            // 合并单元格后的标题行，使用默认标题样式
            writer.merge(6, "危化品追溯记录");
            // 一次性写出内容，使用默认样式，强制输出标题
            writer.write(rows, true);
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
            String fileName = new String("危化品追溯记录.xls".getBytes(), "ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename= " +fileName);
            ServletOutputStream out = response.getOutputStream();
            writer.flush(out, true);
            // 关闭writer，释放内存
            writer.close();
            //此处记得关闭输出Servlet流
            IoUtil.close(out);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
